import { message } from 'antd';
import api from 'api';
import { history } from 'umi';
const { queryArticleByArticleId, queryComment, likeArticle, disLikeArticle, collectArticle, followUser,
    addComment, likeComment, disLikeComment } = api;

export default {
    namespace: 'articleDetail',
    state: {
        articleDetail: {},
        commentList: [],
    },

    subscriptions: {
        setupHistory({ dispatch, history }) {  // eslint-disable-line
            return history.listen(({ pathname, state }) => {
                if (pathname === '/homePage/articleDetail') {
                    dispatch({ type: "queryArticleByArticleId" });
                    dispatch({ type: "queryComment" });
                    dispatch({ type: "getCollectionList" });
                    return;
                }
            })
        },
    },

    effects: {
        * queryArticleByArticleId({ }, { put, call, select }) {
            const userId = yield select(state => state.user.userInfo.userId);
            const articleId = history.location.state?.articleId;
            if (!articleId) {
                return;
            }
            const payload = { articleId, userId };
            const result = yield call(queryArticleByArticleId, payload);
            if (result.code === 0) {
                yield put({ type: "updateState", payload: { articleDetail: result.data, page: 1, pageSize: 10 } });
            }
            else {
                message.error(result.message, 1);
            }

        },
        *queryComment({ }, { call, put, select }) {
            const articleId = history.location.state?.articleId;
            const { userId } = yield select(_ => _.user.userInfo)
            if (!articleId) {
                return;
            }
            const payload = { articleId, userId };
            const result = yield call(queryComment, payload);
            if (result.code === 0) {
                yield put({ type: "updateState", payload: { commentList: result.data } });
            }
            else {
                message.error(result.message, 2);
            }
        },
        *addComment({ payload }, { call, put, select }) {
            const result = yield call(addComment, payload);
            if (result.code === 0) {
                message.success('评论成功', 1);
                yield put({ type: "queryComment" });
            }
            else {
                message.error(result.message, 2);
            }
        },
        *getCollectionList({ }, { call, put, select }) {
            const { userId, userType } = yield select(state => state.user.userInfo);
            if (userId < 0 || userType !== 1) {
                return;
            }
            const collectionList = yield select(state => state.favorite.collectionList);
            if (!collectionList || collectionList.length <= 0) {
                yield put({ type: "favorite/queryCollection" })
            }
        },
        *likeArticle({ payload }, { call, put, select }) {
            const result = yield call(likeArticle, payload);
            if (result.code === 0) {
                if (!payload.isLike) {
                    message.success('取消点赞成功', 1);
                }
                else {
                    message.success('点赞成功', 1);
                }
                yield put({ type: "queryArticleByArticleId" });
            }
            else {
                message.error(result.message, 2);
            }
        },
        *disLikeArticle({ payload }, { call, put, select }) {
            const result = yield call(disLikeArticle, payload);
            if (result.code === 0) {
                if (!payload.isdisLike) {
                    message.success('取消踩成功', 1);
                }
                else {
                    message.success('踩成功', 1);
                }
                yield put({ type: "queryArticleByArticleId" });
            }
            else {
                message.error(result.message, 3);
            }
        },
        *collectArticle({ payload }, { call, put }) {
            const result = yield call(collectArticle, payload)
            if (result.code === 0) {
                if (!payload.isCollect) {
                    message.success('取消收藏成功', 1);
                }
                else {
                    message.success('收藏成功', 1);
                }
                yield put({ type: "queryArticleByArticleId" })
            }
            else {
                message.error(result.message, 3);
            }
        },
        *followUser({ payload }, { call, put, select }) {
            const result = yield call(followUser, payload);
            if (result.code === 0) {
                if (!payload.isFollow) {
                    message.success('取消关注成功', 1);
                }
                else {
                    message.success('关注成功', 1);
                }
                yield put({ type: "queryArticleByArticleId" });
            }
            else {
                message.error(result.message, 3);
            }
        },
        *likeComment({ payload }, { call, put, select }) {
            const result = yield call(likeComment, payload);
            if (result.code === 0) {
                message.success('点赞成功', 1);
                yield put({ type: "queryComment" });
            }
            else {
                message.error(result.message, 3);
            }
        },
        *disLikeComment({ payload }, { call, put, select }) {
            const result = yield call(disLikeComment, payload);
            if (result.code === 0) {
                message.success('踩成功', 1);
                yield put({ type: "queryComment" });
            }
            else {
                message.error(result.message, 3);
            }
        },
    },
    reducers: {
        updateState(state, action) {
            return { ...state, ...action.payload };
        }
    }


}